{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "25bce5eb-8599-40fe-947e-4932cfae8184",
   "metadata": {},
   "source": [
    "# Vald\n",
    "\n",
    "> [Vald](https://github.com/vdaas/vald) is a highly scalable distributed fast approximate nearest neighbor (ANN) dense vector search engine.\n",
    "\n",
    "This notebook shows how to use functionality related to the `Vald` database.\n",
    "\n",
    "To run this notebook you need a running Vald cluster.\n",
    "Check [Get Started](https://github.com/vdaas/vald#get-started) for more information.\n",
    "\n",
    "See the [installation instructions](https://github.com/vdaas/vald-client-python#install)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f45f46f2-7229-4859-9797-30bbead1b8e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install vald-client-python"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f65caa9-8383-409a-bccb-6e91fc8d5e8f",
   "metadata": {},
   "source": [
    "## Basic Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eab0b1e4-9793-4be7-a2ba-e4455c21ea22",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.document_loaders import TextLoader\n",
    "from langchain.embeddings import HuggingFaceEmbeddings\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain.vectorstores import Vald\n",
    "\n",
    "raw_documents = TextLoader(\"state_of_the_union.txt\").load()\n",
    "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
    "documents = text_splitter.split_documents(raw_documents)\n",
    "embeddings = HuggingFaceEmbeddings()\n",
    "db = Vald.from_documents(documents, embeddings, host=\"localhost\", port=8080)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0a6797c-2bb0-45db-a636-5d2437f7a4c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "query = \"What did the president say about Ketanji Brown Jackson\"\n",
    "docs = db.similarity_search(query)\n",
    "docs[0].page_content"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4c4e06d-6def-44ce-ac9a-4c01673c29a2",
   "metadata": {},
   "source": [
    "### Similarity search by vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1eb72610-d451-4158-880c-9f0d45fa5909",
   "metadata": {},
   "outputs": [],
   "source": [
    "embedding_vector = embeddings.embed_query(query)\n",
    "docs = db.similarity_search_by_vector(embedding_vector)\n",
    "docs[0].page_content"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d33588d4-67c2-4bd3-b251-76ae783cbafb",
   "metadata": {},
   "source": [
    "### Similarity search with score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a41e382-0336-4e6d-b2ef-44cc77db2696",
   "metadata": {},
   "outputs": [],
   "source": [
    "docs_and_scores = db.similarity_search_with_score(query)\n",
    "docs_and_scores[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57f930f2-41a0-4795-ad9e-44a33c8f88ec",
   "metadata": {},
   "source": [
    "## Maximal Marginal Relevance Search (MMR)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4790e437-3207-45cb-b121-d857ab5aabd8",
   "metadata": {},
   "source": [
    "In addition to using similarity search in the retriever object, you can also use `mmr` as retriever."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "495754b1-5cdb-4af6-9733-f68700bb7232",
   "metadata": {},
   "outputs": [],
   "source": [
    "retriever = db.as_retriever(search_type=\"mmr\")\n",
    "retriever.get_relevant_documents(query)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e213d957-e439-4bd6-90f2-8909323f5f09",
   "metadata": {},
   "source": [
    "Or use `max_marginal_relevance_search` directly:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99d928d0-3b79-4588-925e-32230e12af47",
   "metadata": {},
   "outputs": [],
   "source": [
    "db.max_marginal_relevance_search(query, k=2, fetch_k=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7dc7ce16-35af-49b7-8009-7eaadb7abbcb",
   "metadata": {},
   "source": [
    "## Example of using secure connection\n",
    "In order to run this notebook, it is necessary to run a Vald cluster with secure connection.\n",
    "\n",
    "Here is an example of a Vald cluster with the following configuration using [Athenz](https://github.com/AthenZ/athenz) authentication.\n",
    "\n",
    "ingress(TLS) -> [authorization-proxy](https://github.com/AthenZ/authorization-proxy)(Check athenz-role-auth in grpc metadata) -> vald-lb-gateway"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6894c02d-7a86-4600-bab1-f7e9cce79333",
   "metadata": {},
   "outputs": [],
   "source": [
    "import grpc\n",
    "\n",
    "with open(\"test_root_cacert.crt\", \"rb\") as root:\n",
    "    credentials = grpc.ssl_channel_credentials(root_certificates=root.read())\n",
    "\n",
    "# Refresh is required for server use\n",
    "with open(\".ztoken\", \"rb\") as ztoken:\n",
    "    token = ztoken.read().strip()\n",
    "\n",
    "metadata = [(b\"athenz-role-auth\", token)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc15c20b-485d-435e-a2ec-c7dcb9db40b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.document_loaders import TextLoader\n",
    "from langchain.embeddings import HuggingFaceEmbeddings\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain.vectorstores import Vald\n",
    "\n",
    "raw_documents = TextLoader(\"state_of_the_union.txt\").load()\n",
    "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
    "documents = text_splitter.split_documents(raw_documents)\n",
    "embeddings = HuggingFaceEmbeddings()\n",
    "\n",
    "db = Vald.from_documents(\n",
    "    documents,\n",
    "    embeddings,\n",
    "    host=\"localhost\",\n",
    "    port=443,\n",
    "    grpc_use_secure=True,\n",
    "    grpc_credentials=credentials,\n",
    "    grpc_metadata=metadata,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "069b96c6-6db2-46ce-a820-24e8933156a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "query = \"What did the president say about Ketanji Brown Jackson\"\n",
    "docs = db.similarity_search(query, grpc_metadata=metadata)\n",
    "docs[0].page_content"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8327accb-6776-4a20-a325-b5da92e3a049",
   "metadata": {},
   "source": [
    "### Similarity search by vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0ab2a97-83e4-490d-81a5-8aaa032d8811",
   "metadata": {},
   "outputs": [],
   "source": [
    "embedding_vector = embeddings.embed_query(query)\n",
    "docs = db.similarity_search_by_vector(embedding_vector, grpc_metadata=metadata)\n",
    "docs[0].page_content"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3f987bd-512e-4e29-acb3-e110e74b51a2",
   "metadata": {},
   "source": [
    "### Similarity search with score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88dd39bc-8764-4a8c-ac89-06e2341aefa6",
   "metadata": {},
   "outputs": [],
   "source": [
    "docs_and_scores = db.similarity_search_with_score(query, grpc_metadata=metadata)\n",
    "docs_and_scores[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fef1bd41-484e-4845-88a9-c7f504068db0",
   "metadata": {},
   "source": [
    "### Maximal Marginal Relevance Search (MMR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cf08477-87b0-41ac-9536-52dec1c5d67f",
   "metadata": {},
   "outputs": [],
   "source": [
    "retriever = db.as_retriever(\n",
    "    search_kwargs={\"search_type\": \"mmr\", \"grpc_metadata\": metadata}\n",
    ")\n",
    "retriever.get_relevant_documents(query, grpc_metadata=metadata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f994fa57-53e4-4fe6-9418-59a5136c6fe8",
   "metadata": {},
   "source": [
    "Or:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2111ce42-07c7-4ccc-bdbf-459165e3a410",
   "metadata": {},
   "outputs": [],
   "source": [
    "db.max_marginal_relevance_search(query, k=2, fetch_k=10, grpc_metadata=metadata)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
